package com.mflex.mwms.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.FullCompareAdapter;
import com.mflex.mwms.model.CompareRecord;
import com.mflex.mwms.model.CompareRequest;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.Inventoryproduct;
import com.mflex.mwms.model.Inventoryproductdetail;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.CollectionUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FullCompareActivity extends BaseActivity {

    @BindView(R.id.stv_BoxNumber)
    EditText stvBoxNumber;

    @BindView(R.id.stv_Package)
    EditText stvPackage;

    @BindView(R.id.button_clear)
    Button buttonClear;

    @BindView(R.id.button_submit)
    Button buttonSubmit;

    @BindView(R.id.lvPackageList)
    ListView lvPackageList;

    MiniLoadingDialog mMiniLoadingDialog;

    Inventoryproduct inventoryproduct;
    List<Inventoryproductdetail> inventoryproductdetailList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_compare);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stvBoxNumber.setOnKeyListener(this);
        stvPackage.setOnKeyListener(this);
        buttonClear.setOnClickListener(this);
        buttonSubmit.setOnClickListener(this);

        inventoryproduct = new Inventoryproduct();
        inventoryproductdetailList = new ArrayList<>();
        FullCompareAdapter fullCompareAdapter = new FullCompareAdapter(this, inventoryproductdetailList);
        lvPackageList.setAdapter(fullCompareAdapter);
        stvBoxNumber.requestFocus();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button_clear:
                clearButtonMd(view);
                break;
            case R.id.button_submit:
                submitButtonMd(view);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_BoxNumber:
                    checkBoxNumber(view);
                    break;
                case R.id.stv_Package:
                    requestPackage(view);
                    break;
                default:
                    break;
            }
        }
        return false;
    }

    private void checkBoxNumber(View v) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
//            XToastUtils.error("箱号未输入", 2500);
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "箱号未输入");
            return;
        }

        mMiniLoadingDialog.show();
        XHttp.get("api/Comparison/GetFullInventoryDetail")
                .params("BoxNumber", stvBoxNumber.getText().toString())
                .params("WareHouseName", MMKVUtils.getString("Warehose", ""))
                .params("TaskStatusName", 0)
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<Inventoryproduct>, Inventoryproduct>(new SimpleCallBack<Inventoryproduct>() {

                    @Override
                    public void onSuccess(Inventoryproduct response) {
                        mMiniLoadingDialog.dismiss();
                        inventoryproduct = response;
                        inventoryproductdetailList = response.getList();
                        FullCompareAdapter fullCompareAdapter = new FullCompareAdapter(FullCompareActivity.this, inventoryproductdetailList);
                        lvPackageList.setAdapter(fullCompareAdapter);

                        stvPackage.setText("");
                        stvPackage.requestFocus();

                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
                        MessageUtils.errorMsgAndVoice(FullCompareActivity.this, e);
                    }
                }) {
                });
    }

    private void requestPackage(View v) {
        if (StringUtils.isEmpty(stvBoxNumber.getText().toString())) {
//            XToastUtils.error("箱号未输入", 3000);
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "箱号未输入");
            return;
        }
        if (CollectionUtils.isEmpty(inventoryproductdetailList)) {
//            XToastUtils.error("对比列表为空", 3000);
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "对比列表为空");
            return;
        }

        boolean checkFlag = false;
        boolean existFlag = false;
        for (Inventoryproductdetail inventoryproductdetail : inventoryproductdetailList) {
            if (StringUtils.equals(inventoryproductdetail.getPackage(), stvPackage.getText().toString())) {
                if (inventoryproductdetail.checked) {
                    checkFlag = true;
                } else {
                    inventoryproductdetail.setChecked(true);
                }
            }
        }

        if (checkFlag) {
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "该包已扫描");
        }

        for (Inventoryproductdetail inventoryproductdetail : inventoryproductdetailList) {
            if (StringUtils.equals(inventoryproductdetail.getPackage(), stvPackage.getText().toString())) {
                existFlag = true;
            }
        }
        if (!existFlag) {
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "该包不在列表中");
        }
        FullCompareAdapter fullCompareAdapter = new FullCompareAdapter(this, inventoryproductdetailList);
        lvPackageList.setAdapter(fullCompareAdapter);
        stvPackage.setText("");
        stvPackage.postDelayed(new Runnable() {
            @Override
            public void run() {
                stvPackage.requestFocus();
            }
        }, 200);
    }

    private void clearButtonMd(View v) {
        stvBoxNumber.setText("");
        stvBoxNumber.requestFocus();
        stvPackage.setText("");
        inventoryproductdetailList = new ArrayList<>();
        FullCompareAdapter fullCompareAdapter = new FullCompareAdapter(this, inventoryproductdetailList);
        lvPackageList.setAdapter(fullCompareAdapter);
    }

    private void submitButtonMd(View v) {
        boolean compareFlag = true;
        if (CollectionUtils.isEmpty(inventoryproductdetailList)) {
//            XToastUtils.error("对比列表为空", 3000);
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "对比列表为空");
            return;
        }

        for (Inventoryproductdetail inventoryproductdetail : inventoryproductdetailList) {
            if (!inventoryproductdetail.checked) {
                compareFlag = false;
                break;
            }
        }

        if (!compareFlag) {
//            XToastUtils.error("未全部对比", 3000);
            MessageUtils.errorMsgAndVoice(FullCompareActivity.this, "未全部对比");
            return;
        }

        CompareRequest compareRequest = new CompareRequest();
        List<CompareRecord> compareRecordList = new ArrayList<>();
        for (Inventoryproductdetail inventoryproductdetail : inventoryproductdetailList) {
            CompareRecord compareRecord = new CompareRecord();
            compareRecord.setBoxNumber(stvBoxNumber.getText().toString());
            compareRecord.setPackage(inventoryproductdetail.getPackage());
            compareRecordList.add(compareRecord);
        }
        compareRequest.setCompareRecords(compareRecordList);

        mMiniLoadingDialog.show();
        XHttp.post("api/Comparison/FullCompare")
                .upJson(JsonUtil.toJson(compareRequest))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<Boolean>, Boolean>(new SimpleCallBack<Boolean>() {

                    @Override
                    public void onSuccess(Boolean response) {
                        mMiniLoadingDialog.dismiss();
                        XToastUtils.success("比对成功", 2000);
                        stvBoxNumber.setText("");
                        stvBoxNumber.requestFocus();
                        stvPackage.setText("");
                        inventoryproductdetailList = new ArrayList<>();
                        FullCompareAdapter fullCompareAdapter = new FullCompareAdapter(FullCompareActivity.this, inventoryproductdetailList);
                        lvPackageList.setAdapter(fullCompareAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MessageUtils.errorMsgAndVoice(FullCompareActivity.this, e);
                    }
                }) {
                });
    }


}